///<reference path="./index.d.ts"/>
export default class Time {
    constructor(params = {}) {
        this.duration = 1000;
        this.afterPeriod = undefined;
        this.dateSeparator = '-';
        this.timeSeparator = ':';
        this.finally = undefined;
        this.times = 60;
        this.stamp = 0;
        this.isFirstPeriod = false;
        this._id = 0;
        this._isRunning = false;
        Object.assign(this, params);
        this._date = params.date || new Date();
        this.init();
    }
    init() {
        this._isRunning = false;
        this.stamp = this._date.getTime();
    }
    // date相关
    get year() { return this._date.getFullYear(); }
    get month() { return this._date.getMonth() + 1; }
    get day() { return this._date.getDate(); }
    get dateStr() {
        return this.year + this.dateSeparator + Time.padStart(this.month) + this.dateSeparator + Time.padStart(this.day);
    }
    get DateStrZh() {
        return this.year + '年' + Time.padStart(this.month) + '月' + Time.padStart(this.day) + '日';
    }
    // get date (): string {return this.dateStr}
    // 时间相关
    get hours() { return this._date.getHours(); }
    get minutes() { return this._date.getMinutes(); }
    get seconds() { return this._date.getSeconds(); }
    get timeSign() {
        return this.hours <= 12 ? 'AM' : 'PM';
    }
    get timeStr() {
        return Time.padStart(this.hours) + this.timeSeparator + Time.padStart(this.minutes) + this.timeSeparator + Time.padStart(this.seconds);
    }
    get timeStrZh() {
        return Time.padStart(this.hours) + '时' + Time.padStart(this.minutes) + '分' + Time.padStart(this.seconds) + '秒';
    }
    get time() { return this.timeStr; }
    // 获取星期
    week(type = 'cn') {
        if (type === 'cn') {
            return Time.weekMap[this._date.getDay()];
        }
        return this._date.getDay();
    }
    // padStart
    static padStart(value, length = 2, str = '0') {
        value = '' + value;
        if (value.length > length)
            return value;
        if (String.prototype['padStart']) {
            return (value).padStart(length, str);
        }
        let arr = [];
        arr.length = length - value.length;
        for (let i = arr.length; i > 0; i--) {
            arr[i - 1] = str;
        }
        return arr.join('') + value;
    }
    start() {
        this._isRunning = true;
        this.interval();
    }
    stop() {
        this._isRunning = false;
        clearTimeout(this._id);
        console.log(`time is up`);
    }
    countDownStart() {
        let self = this;
        let cb = this.afterPeriod;
        this.afterPeriod = function () {
            if (this.times >= 0) {
                self.times -= 1;
                cb && (cb(self));
            }
            if (this.times < 0)
                this.stop();
        };
        this.start();
    }
    interval() {
        let self = this;
        if (this.isFirstPeriod && this.afterPeriod) {
            this.isFirstPeriod = false;
            this.afterPeriod(self);
        }
        if (self._isRunning) {
            this._id = setTimeout(() => {
                self._date = new Date();
                self.afterPeriod && self.afterPeriod(self);
                self.interval();
            }, self.duration);
        }
        else {
            self.finally && self.finally(self);
            return clearTimeout(self._id);
        }
    }
    /** 默认同String*/
    toString() {
        return `${this.dateStr} ${this.timeStr}`;
    }
    /**中文字符串 */
    toStringZh() {
        return `${this.DateStrZh + this.timeStrZh}`;
    }
}
Time.weekMap = ['日', '一', '二', '三', '四', '五', '六'];
